﻿using FlaUI.Core.AutomationElements;
using FlaUI.Core.Conditions;
using FlaUI.Core.Input;
using FlaUI.Core.WindowsAPI;
using FlaUI.UIA3;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsApp1
{
    public partial class BeiKe : Form
    {
        public BeiKe()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var ids = richTextBox1.Text.Split(',');

            Process[] processes = Process.GetProcessesByName("msedge").OrderBy(ex => ex.StartTime).ToArray();

            //1.附加到微信进程
            using (var app = FlaUI.Core.Application.Attach(processes.First().Id))
            {
                using (var automation = new UIA3Automation())
                {
                    for (int i = 0; i < ids.Length; i++)
                    {
                        try
                        {
                            //2.获取主界面
                            var mainWindow = app.GetMainWindow(automation);
                            Console.WriteLine("获取主界面");

                            //3.切换到地址栏
                            var Obj = mainWindow.FindAllByXPath("/Pane[2]/Pane/Pane[1]/Pane[2]/Pane[1]/ToolBar[1]/Group/Edit").FirstOrDefault();
                            //Obj.DrawHighlight(System.Drawing.Color.Red);
                            Obj.Click();
                            System.Threading.Thread.Sleep(100);

                            //删除内容
                            Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_A });//全选  
                            Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.DELETE });//删除 
                            System.Threading.Thread.Sleep(100);

                            //5.输入地址
                            //var url = "https://ajax.api.ke.com/house/house/getbuildings?community_id=" + ids[i];
                            var url = "https://sy.zu.ke.com/aj/resblock/building?resblock_id=" + ids[i];
                            System.Windows.Forms.Clipboard.SetText(url);//内容复制到粘贴板
                            Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });//ctrl+v 

                            //6.回车请求
                            Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER });//回车   
                            System.Threading.Thread.Sleep(500);

                            //7.获取内容
                            var Obj1 = mainWindow.FindAllByXPath("/Pane[2]/Pane/Pane[1]/Pane[2]/Pane[2]/Pane/Pane/Pane/Document/Text").FirstOrDefault();
                            //var Obj1 = mainWindow.FindAllByXPath("Pane[2]/Pane/Pane[1]/Pane[2]/Pane[2]/Pane/Pane/Pane/Document[2]/Text").FirstOrDefault();
                            //Obj1.DrawHighlight(System.Drawing.Color.Red);
                            var nr = Obj1.Name;

                            //CommonLib.FileHelper.FileWrite($"d://ke/{i}_{ids[i]}.txt", nr);
                            System.Threading.Thread.Sleep(300);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }
    }
}
